<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 运能设置类
 * @package  module
 * @subpackage  base/system
 * @author   create by allen.qiang
 * @version 1.0
 */

class sysMax {

    /**
     * 查询配送运能时间
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function searchGoodsSpace( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        if (isset($arg_array['uuid']) && is_numeric($arg_array['uuid'])) {
            $sql .= ' AND uuid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['uuid'];
        }

        //配送开始时间
        if (isset($arg_array['send_start_time']) && is_numeric($arg_array['send_start_time'])) {
            $sql .= ' AND send_start_time = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['send_start_time'];
        }

        //配送结束时间
        if (isset($arg_array['send_end_time']) && is_numeric($arg_array['send_end_time'])) {
            $sql .= ' AND send_end_time = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['send_end_time'];
        }

        //运单上限
        if (isset($arg_array['max_bill']) && is_numeric($arg_array['max_bill'])) {
            $sql .= ' AND max_bill = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['max_bill'];
        }

        //状态
        if (isset($arg_array['send_status']) && is_numeric($arg_array['send_status'])) {
            $sql .= ' AND send_status = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['send_status'];
        }


        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_distribution_send ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_distribution_send ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'uuid' => $item->uuid,
                    'creator' => $item->creator,
                    'distribution_id' => $item->distribution_id,
                    'send_start_time' => $item->send_start_time,
                    'send_end_time' => $item->send_end_time,
                    'max_bill' => $item->max_bill,
                    'send_status' => $item->send_status
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 添加运能设置
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function addDistributionSend ( $arg_array ) {



    }

}